<?php
/*
 * code used to generate a grid of timesheet lines in a user's timesheet 
*/
// Create connection
include("../inc_files/utils/dbconnection.php");
$startDate="";
//get the start date, end date, and details e.g. is the day closed (bank hol etc.)
//or is the day blocked (also appears in previous year's timesheet) of the timesheet period
$query ="SELECT 
    timesheet_periods.Closed, timesheet_periods.SunStatus, timesheet_periods.MonStatus,
    timesheet_periods.TueStatus, timesheet_periods.WedStatus, timesheet_periods.ThurStatus,
    timesheet_periods.FriStatus, timesheet_periods.SatStatus, timesheet_periods.StartDate
FROM
    timesheet_periods, timesheets
WHERE
    timesheets.TimesheetID = ? AND timesheets.TimesheetPeriod = timesheet_periods.PeriodID;";
//bind the customerid to the query

if($stmt = $mysqli -> prepare($query))	{
	$stmt -> bind_param("i", $timesheetID);
	$timesheetID = $mysqli->real_escape_string(trim($_GET["timesheetid"]));
	//execute the query
	$result = $stmt -> execute();
	//store the results
	$stmt->store_result();
	//bind the results to variables
	$stmt -> bind_result($periodClosed, $sunStatus, $monStatus, $tueStatus,
			$wedStatus, $thurStatus, $friStatus, $satStatus, $startDate);
	//actually fetch the results
	$stmt->fetch();
	//close the statement
	$stmt -> close();
}

//query for all timesheet lines and related tasks
$query ="SELECT 
    tasks.Title, projects.ProjectTitle, projects.ProjectID, timesheet_lines.TimesheetLineID,
    timesheet_lines.SunHours, timesheet_lines.MonHours, timesheet_lines.TueHours, timesheet_lines.WedHours, 
    timesheet_lines.ThurHours, timesheet_lines.FriHours, timesheet_lines.SatHours
FROM
    timesheet_lines
        INNER JOIN
    tasks ON timesheet_lines.Task = tasks.TaskID
        INNER JOIN
    projects ON tasks.Project = projects.ProjectID
WHERE
    timesheet_lines.Timesheet = {$_GET["timesheetid"]}";
$result = $mysqli->query( $query );

//count the number of returned results
$resultCount = $result->num_rows;

//set up variables to specify css classes for closed, blocked or normal days
$working_day_statuses = array("blocked", "working", "nonworking");
//table cells
$sunStatusTd = "tstd".$working_day_statuses[$sunStatus-1];
$monStatusTd = "tstd".$working_day_statuses[$monStatus-1];
$tueStatusTd = "tstd".$working_day_statuses[$tueStatus-1];
$wedStatusTd = "tstd".$working_day_statuses[$wedStatus-1];
$thurStatusTd = "tstd".$working_day_statuses[$thurStatus-1];
$friStatusTd = "tstd".$working_day_statuses[$friStatus-1];
$satStatusTd = "tstd".$working_day_statuses[$satStatus-1];
//hide the form field if day is blocked
$sunStatusFf = "tsff".$working_day_statuses[$sunStatus-1];
$monStatusFf = "tsff".$working_day_statuses[$monStatus-1];
$tueStatusFf = "tsff".$working_day_statuses[$tueStatus-1];
$wedStatusFf = "tsff".$working_day_statuses[$wedStatus-1];
$thurStatusFf = "tsff".$working_day_statuses[$thurStatus-1];
$friStatusFf = "tsff".$working_day_statuses[$friStatus-1];
$satStatusFf = "tsff".$working_day_statuses[$satStatus-1];


//if there are some results returned create a table showing the results
$rownumber = 0;

echo "<table class=\"datagrid\">";
echo "<tr class=\"datagrid headerrow\">";
echo "<th style=\"width:20%\">Project</th>";
echo "<th style=\"width:40%\">Task</th>";
echo "<th style=\"width:5%\">Sun ".date('d/m', strtotime($startDate))."</th>";
echo "<th style=\"width:5%\">Mon ".date('d/m',strtotime(" +1 day", strtotime($startDate)))."</th>";
echo "<th style=\"width:5%\">Tue ".date('d/m',strtotime(" +2 day", strtotime($startDate)))."</th>";
echo "<th style=\"width:5%\">Wed ".date('d/m',strtotime(" +3 day", strtotime($startDate)))."</th>";
echo "<th style=\"width:5%\">Thur ".date('d/m',strtotime(" +4 day", strtotime($startDate)))."</th>";
echo "<th style=\"width:5%\">Fri ".date('d/m',strtotime(" +5 day", strtotime($startDate)))."</th>";
echo "<th style=\"width:5%\">Sat ".date('d/m',strtotime(" +6 day", strtotime($startDate)))."</th>";
echo "<th style=\"width:5%\">&nbsp;</th>

		</tr>";
if( $resultCount > 0){
	while($row = mysqli_fetch_array($result))
	{
		if($rownumber %2 == 0)
		{
			echo "<tr class=\"datagrid evenrow\">";
		}
		else if ($rownumber %2)
		{
			echo "<tr class=\"datagrid oddrow\">";
		}



		echo "<td style=\"width:20%\">" .$row['ProjectID'] . "</td>";
		echo "<td style=\"width:40%\">" .$row['Title']. "</td>";
		echo "<td class=\"{$sunStatusTd}\"><input type=\"text\" name=\"Sun#{$row['TimesheetLineID']}\" id=\"Sun#{$row['TimesheetLineID']}\" value=\"{$row['SunHours']}\" size=\"3\" class=\"{$sunStatusFf}\" onfocus=\"temporaryFieldValue = this.value\" onchange=\"validateHoursFields(this)\"></td>";
		echo "<td class=\"{$monStatusTd}\"><input type=\"text\" name=\"Mon#{$row['TimesheetLineID']}\" id=\"Mon#{$row['TimesheetLineID']}\" value=\"{$row['MonHours']}\" size=\"3\" class=\"{$monStatusFf}\" onfocus=\"temporaryFieldValue = this.value\" onchange=\"validateHoursFields(this)\"></td>";
		echo "<td class=\"{$tueStatusTd}\"><input type=\"text\" name=\"Tue#{$row['TimesheetLineID']}\" id=\"Tue#{$row['TimesheetLineID']}\" value=\"{$row['TueHours']}\" size=\"3\" class=\"{$tueStatusFf}\" onfocus=\"temporaryFieldValue = this.value\" onchange=\"validateHoursFields(this)\"></td>";
		echo "<td class=\"{$wedStatusTd}\"><input type=\"text\" name=\"Wed#{$row['TimesheetLineID']}\" id=\"Wed#{$row['TimesheetLineID']}\" value=\"{$row['WedHours']}\" size=\"3\" class=\"{$wedStatusFf}\" onfocus=\"temporaryFieldValue = this.value\" onchange=\"validateHoursFields(this)\"></td>";
		echo "<td class=\"{$thurStatusTd}\"><input type=\"text\" name=\"Thur#{$row['TimesheetLineID']}\" id=\"Thur#{$row['TimesheetLineID']}\" value=\"{$row['ThurHours']}\" size=\"3\" class=\"{$thurStatusFf}\" onfocus=\"temporaryFieldValue = this.value\" onchange=\"validateHoursFields(this)\"></td>";
		echo "<td class=\"{$friStatusTd}\"><input type=\"text\" name=\"Fri#{$row['TimesheetLineID']}\" id=\"Fri#{$row['TimesheetLineID']}\" value=\"{$row['FriHours']}\" size=\"3\" class=\"{$friStatusFf}\" onfocus=\"temporaryFieldValue = this.value\" onchange=\"validateHoursFields(this)\"></td>";
		echo "<td class=\"{$satStatusTd}\"><input type=\"text\" name=\"Sat#{$row['TimesheetLineID']}\" id=\"Sat#{$row['TimesheetLineID']}\" value=\"{$row['SatHours']}\" size=\"3\" class=\"{$satStatusFf}\" onfocus=\"temporaryFieldValue = this.value\" onchange=\"validateHoursFields(this)\"></td>";
		if($periodClosed != 1){
		echo "<td style=\"width:5%\"><img height=\"32\" width=\"32\"  src=\"../images/edit-delete-2-32.png\" alt=\"Delete records for ".
				$row['Title'] ."\" onclick=\"deleteline('"  . $row['TimesheetLineID'] . "')\" style=\"cursor:pointer\"></td>";
		} else {
			echo "<td style=\"width:20%\">&nbsp;</td>";
		}
		echo "</tr>";
		$rownumber++;
	}

}
//tidy up database connection
$mysqli->close();
//add the totals row
echo "<tr class=\"datagrid totalsrow\">";
echo "<td colspan=\"2\" style=\"width:60%; text-align: right\">Totals&nbsp;&nbsp;&nbsp;</td>";
echo "<td style=\"width:5%\"><input type=\"text\" id=\"TotalSun\" value=\"0\" size=\"3\" style=\"text-align: center; ";
if($sunStatus == 1) echo "visibility:hidden;";
echo"\" readonly></td>";
echo "<td style=\"width:5%\"><input type=\"text\" id=\"TotalMon\" value=\"0\" size=\"3\" style=\"text-align: center; ";
if($monStatus == 1) echo "visibility:hidden;";
echo"\" readonly></td>";
echo "<td style=\"width:5%\"><input type=\"text\" id=\"TotalTue\" value=\"0\" size=\"3\" style=\"text-align: center; ";
if($tueStatus == 1) echo "visibility:hidden;";
echo"\" readonly></td>";
echo "<td style=\"width:5%\"><input type=\"text\" id=\"TotalWed\" value=\"0\" size=\"3\" style=\"text-align: center; ";
if($wedStatus == 1) echo "visibility:hidden;";
echo"\" readonly></td>";
echo "<td style=\"width:5%\"><input type=\"text\" id=\"TotalThur\" value=\"0\" size=\"3\" style=\"text-align: center; ";
if($thurStatus == 1) echo "visibility:hidden;";
echo"\" readonly></td>";
echo "<td style=\"width:5%\"><input type=\"text\" id=\"TotalFri\" value=\"0\" size=\"3\" style=\"text-align: center; ";
if($friStatus == 1) echo "visibility:hidden;";
echo"\" readonly></td>";
echo "<td style=\"width:5%\"><input type=\"text\" id=\"TotalSat\" value=\"0\" size=\"3\" style=\"text-align: center; ";
if($satStatus == 1) echo "visibility:hidden;";
echo"\" readonly></td>";
echo "<td style=\"width:5%\">&nbsp;</td>";
echo "</tr>";

//add the new item row (hidden at first) and the add new row link
echo "<tr id=\"newitemrow\" class=\"datagrid hidenewitem\">";
echo "<td colspan=\"2\"><select name=\"projectddl\" id=\"projectddl\" style=\"width: 400px\" onchange=\"processddl({$timesheetID})\">";
include("../inc_files/ui/timesheetprojectsddl.php");
echo"</select></td>";
echo "<td colspan=\"6\"><div id=\"ddltasks\"></div></td>";
echo "<td style=\"width:5%\"><img height=\"32\" width=\"32\" id=\"tasksubmitimage\"  src=\"../images/apply_32.png\" alt=\"Submit\"  onclick=\"addtimesheetline();\" style=\"cursor:pointer; visibility:hidden;\"></td>";
echo "<td style=\"width:5%\"><img height=\"32\" width=\"32\"  src=\"../images/cancel_32.png\" alt=\"Cancel\"  onclick=\"togglenewitem();\" style=\"cursor:pointer\"></td>";
echo "</tr>";




echo "<tr class=\"datagrid footer\">";
//if the timesheet period is not closed then allow the user to save changes and add new lines
if($periodClosed != 1){
echo "<td colspan=\"4\">";

echo "<a href=\"javascript:;\" onclick='savetimesheet();'>Save 
		<img src=\"../images/save_32.png\" style=\"border:none\" height=\"32\" width=\"32\" alt=\" Add new task row.\" ></a></td>";

echo "</td>";
echo "<td colspan=\"4\">
		<a href=\"javascript:;\" onclick='togglenewitem();'>Add new timesheet line
		<img src=\"../images/add_32.png\" style=\"border:none\" height=\"32\" width=\"32\" alt=\" Add new task row.\" ></a></td>";

echo "</td>
		<td colspan=\"2\" style=\"width:30%\"></td>";
		
		} else {
		//otherwise the user cannot change or save the timesheet
echo "<td colspan=\"10\"></td>";
		}
		
echo "</tr>";


echo "</table>";

?>